﻿namespace ExamV5.LineStudyExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;

    public class JoinExamS : Page
    {
        private bool bool_0 = true;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected int intPaperID = 0;
        private PublicFun publicFun_0 = new PublicFun();
        protected string strLoadInfo = "0";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                this.intPaperID = Convert.ToInt32(base.Request["PaperID"].ToString());
                this.strLoadInfo = base.Request["LoadInfo"].ToString();
                base.Response.Expires = 0;
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    base.Response.Write("<script>window.location.href='#'</script>");
                    string str6 = "";
                    string str5 = "";
                    string str4 = "";
                    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                    connection.Open();
                    SqlCommand command = new SqlCommand(string.Concat(new object[] { "select b.JoinExamNum,a.JoinExamNum as ExamNum,a.StartTime,a.EndTime,a.ExamType,a.Flag,a.ExamName from ExamUser a,PaperInfo b where b.ID=a.PaperID and a.UserCode='", this.httpCookie_0.Value.Trim(), "' and a.PaperID=", this.intPaperID }), connection);
                    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    if (reader.Read())
                    {
                        str6 = reader["ExamType"].ToString();
                        str5 = reader["Flag"].ToString();
                        str4 = reader["StartTime"].ToString();
                        reader["EndTime"].ToString();
                        reader["ExamName"].ToString();
                        if ((reader["JoinExamNum"].ToString() != "0") && (Convert.ToInt32(reader["ExamNum"].ToString()) >= Convert.ToInt32(reader["JoinExamNum"].ToString())))
                        {
                            base.Response.Write("<script>alert('对不起，此份试卷考试次数限制已达到上限，无法再操作');window.close();</script>");
                            return;
                        }
                    }
                    reader.Close();
                    command.Dispose();
                    connection.Close();
                    connection.Dispose();
                    if ((str6 == "A") ? !(str5 != "A") : true)
                    {
                        if (str4.Trim() == "")
                        {
                            string str3 = Convert.ToString(base.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
                            switch (str3)
                            {
                                case "":
                                case null:
                                    str3 = Convert.ToString(base.Request.ServerVariables["REMOTE_ADDR"]);
                                    break;
                            }
                            string str2 = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
                            this.publicFun_0.ExecuteSql(string.Concat(new object[] { "delete from Systip where GetNumber='", this.httpCookie_0.Value, "' and Formtable='PaperInfo' and FormID=", this.intPaperID, " ;update ExamUser set StartTime='", str2, "',JoinDate='", DateTime.Now.ToShortDateString(), "',LoginIP='", str3, "' where UserCode='", this.httpCookie_0.Value.Trim(), "' and PaperID=", this.intPaperID }));
                        }
                        string values = this.publicFun_0.GetValues("select MemberFen from PaperInfo where ID=" + this.intPaperID, "MemberFen");
                        if (values.Trim() == "")
                        {
                            values = "0";
                        }
                        if (!(this.publicFun_0.GetValues("select a.UserCode  from UserInfo a,MemberLevel b where getdate()>DateAdd(dd,b.RemoveValue,Convert(dateTime,a.UpdateTime)) and a.UserMember=b.ID and a.UserCode='" + this.httpCookie_0.Value + "' and LevelRemove='C'", "UserCode") == ""))
                        {
                            this.publicFun_0.ExecuteSql("update UserInfo set UserFen=0 where UserCode='" + this.httpCookie_0.Value + "'");
                            base.Response.Write("<script>alert('会员积分不足,充值方可进行考试学习！');window.close();</script>");
                        }
                        else
                        {
                            this.bool_0 = this.publicFun_0.OpertionMemberFen(this.httpCookie_0.Value, Convert.ToInt32(values));
                            if (!this.bool_0)
                            {
                                base.Response.Write("<script>alert('会员积分不足,充值方可进行考试学习！');window.close();</script>");
                            }
                        }
                    }
                    else
                    {
                        base.Response.Write("<script>alert('对不起，此份试卷您正在考试中或已经交卷或停考，无法再操作！');window.close();</script>");
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

